// 导航
let box = document.querySelector(".box");
let nav = document.querySelector(".top");
let btn = document.querySelector(".btn");

// 点击跳转首页
btn.onclick = function () {
  location.href = "../html/shouye.html";
};

let daohang = nav.querySelector(".daohang");
let lis = daohang.querySelectorAll("ul li");
// console.log(lis);

// 点击切换导航栏高亮
lis.forEach(function (item, index) {
  item.onclick = function () {
    if (index == 0) {
      location.href = "../html/shouye.html";
    }
    if (index == 1) {
      location.href = "../html/shouye.html#matter1";
    }
    if (index == 2) {
      location.href = "../html/wisdom.html";
    }
    if (index == 3) {
      location.href = "../html/course.html";
    }
    if (index == 4) {
      location.href = "../html/tool.html";
    }
    if (index == 5) {
      location.href = "../html/case.html";
    }
  };
});

// 回到顶部
let customerService = document.querySelector(".customerService");
let back_top = customerService.querySelector(".back_top");

// 显示回到顶部按钮
window.onscroll = function () {
  if (!(scrollY >= 450)) {
    back_top.style.display = "none";
    return;
  }
  back_top.style.display = "block";
};

// 点击回到顶部
back_top.onclick = function () {
  scrollTo({ top: 0, behavior: "smooth" });
};

// 跳转到注册页面
let content = document.querySelector(".content");
let bdbox = content.querySelector(".bdbox");
let register = bdbox.querySelector(".register");
console.log(register);

register.onclick = function (e) {
  e.returnValue = false;
  location.href = "../html/register.html";
};

//
let form = document.querySelector("form");
let tel = document.querySelector(".userlist");
let pass = document.querySelector(".password");

form.onsubmit = function (e) {
  e = e || window.event;
  e.returnValue = false;

  // console.log(tel.value);
  // console.log(pass.value);

  let xhr = new XMLHttpRequest();
  xhr.open(
    "get",
    `http://localhost:9090/login?tel=${tel.value}&password=${pass.value}`
  );
  xhr.send();
  xhr.onload = function () {
    // console.log(xhr.responseText);
    let data = JSON.parse(xhr.responseText);
    if (!data.code) {
      alert(data.msg);
      return;
    }
    location.href = "../html/shouye.html";
  };
};
